package com.cnhis.cloudhealth.clinical.hosnurse.hospatient.bo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;
import com.cnhis.cloudhealth.clinical.hosnurse.hoscharge.bo.HoschargeBo;
import com.cnhis.cloudhealth.clinical.hosnurse.hoscharge.dao.HoschargeDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hosexpense.bo.HosExpenseBo;
import com.cnhis.cloudhealth.clinical.hosnurse.hospatient.dao.HosPatientDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@SuppressWarnings({"rawtypes","unchecked"})
@Service
public class HosPatientBo {
	@Resource
  private HosPatientDao hosPatientDao;
  @Resource
  private HosExpenseBo HosPatientBo;
  @Resource
  private HoschargeBo  hoschargeBo;
  @Resource
  private HoschargeDao hoschargeDao;
  @Resource
  private GetNewId getNewId;
	private Logger log=Logger.getLogger(HosPatientBo.class);
	private ModelVo packModelVo(String nid,Map map){
		ModelVo mo = new ModelVo();
		List<?> list = null;
		try {
			list = hosPatientDao.queryForList(nid,map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list!=null&&list.size()>0) {
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(list.size());
			mo.setRows(list);
		}
		return mo;
	}
	private ModelVo packModelVoPage(String nid,Map map,int pageno,int pagesize) {
		ModelVo mo = new ModelVo();
		List<?> list = null;
		try {
			Page p = hosPatientDao.queryForPage(nid, map, pageno, pagesize);
			list = p.getList();
			if (list != null && list.size() > 0) {
				mo.setList(list);
				mo.setResult(StaticKeys.OH_SUCCESS);
				mo.setTotal(p.getTotalNumber());
				mo.setRows(list);
				mo.setPage(pageno);
				mo.setRecords(p.getTotalNumber());
				mo.setTotalPage(p.getTotalPage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mo;
	}
	public ModelVo queryOfficeArea(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.queryOfficeArea(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo queryOfficeArea1(int currPage,int pageSize)throws Exception{
		ModelVo mo = new ModelVo();
		Page list =null;
		try {
			list=hosPatientDao.queryOfficeArea1(currPage, pageSize);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list.getList());
		mo.setTotal(list.getTotalNumber());
		return mo;
	}
	
	private Map getParam406_para396()throws Exception{
		return hosPatientDao.getparam406_para396();
	}
	private List groupList(List list,String goupName,String key)throws Exception{
		List dataList = new ArrayList<>();

		int length=list.size();
		for (int i = 0; i < length; i++) {
			Map dataMap = new HashMap();
			Map<String,Object> map1 = (Map<String, Object>) list.get(i);
			List data = new ArrayList<>();
			data.add(map1);
			 for (int j = i+1; j < length; j++) {
				 Map<String,Object> map2 = (Map<String, Object>) list.get(j);
				if(map1.get(goupName).toString().equals(map2.get(goupName).toString())){
					data.add(map2);
					list.remove(j);
					j--;
					length--;
				}
			}
			 dataMap.put("data", data);
			 dataMap.put("name", map1.get(key).toString());
			 dataList.add(dataMap);
		}
		return dataList;
	}
//	public ModelVo inpatientList(Map<String,Object> map,int currPage,int pageSize)throws Exception{
//		 ModelVo mo = new ModelVo();
//			Page pg = new Page();
//
//			try {
//				Map m = getParam406_para396();
//				map.putAll(m);
//				pg = hosPatientDao.inpatientList(map, currPage, pageSize);
//				mo.setRows(pg.getList());
//				mo.setResult(StaticKeys.OH_SUCCESS);
//				mo.setTotal(pg.getTotalNumber());
//				List list = new ArrayList<>();
//				list.addAll(pg.getList());
////				mo.setList(groupList(list,"bck01d","fbck03"));
//				mo.setList(pg.getList());
//				mo.setPage(currPage);
//				mo.setRecords(pg.getTotalNumber());
//				mo.setTotalPage(pg.getTotalPage());
//			} catch (Exception e) {
//				e.printStackTrace();
//				mo.setResult(StaticKeys.OH_FALSE);
//				return mo;
//			}
//
//
//			return mo;
//	}
	@SuppressWarnings("unchecked")
	public ModelVo inpatientList(Map map,int pageno,int pagesize){
		return packModelVoPage("HosPatient.878",map,pageno,pagesize);
	}
	@SuppressWarnings("unchecked")
	public ModelVo selectPatientcount(Map map){
		Integer pType = MapUtils.getInteger(map,"pType",0);
		if(pType==0){
			return packModelVo("HosPatient.878tcount",map);
		}else{
			return packModelVo("HosPatient.7686count",map);
		}
	}
	public ModelVo selectNations(){
		return packModelVo("HosPatient.selectNations",null);
	}
	public ModelVo selectCountries(){
		return packModelVo("HosPatient.selectCountries",null);
	}
	public ModelVo updateBedInfo(){
		ModelVo mo = new ModelVo();
		mo.setResult(StaticKeys.OH_SUCCESS);
		try {
			hosPatientDao.updateBedInfo();			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("更新错误!");
			return mo;
		}
		mo.setResultMsg("更新成功!");
		return mo;
	}
	public ModelVo selectdiagnoses(Map map){
		return packModelVo("HosPatient.selectdiagnoses",map);
	}
	public ModelVo homePagePatient(Map map,int currPage,int pageSize)throws Exception{
		ModelVo mo = new ModelVo();
		Page list =null;
		try {
			map.put("adate", DateUtils.getDateString(new Date()));
			list=hosPatientDao.homePagePatient(map, currPage, pageSize);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		if (list.getList() != null && list.getList().size() > 0) {
            mo.setList(list.getList());
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setTotal(list.getTotalNumber());
            mo.setPage(currPage);
            mo.setRecords(list.getTotalNumber());
            mo.setTotalPage(list.getTotalPage());
        }
		return mo;
	}
	public ModelVo validationDate(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		try {
			if(hosPatientDao.validationDate(map)>0){
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("病人已过期");
				return mo;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		
		mo.setResult(StaticKeys.OH_SUCCESS);
		return mo;
	}
	public ModelVo unSettlePatient(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.unSettlePatient(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo checkInpatientState(Map map)throws Exception{
		ModelVo vo = new ModelVo();
		if(hosPatientDao.checkInpatientState(map)>0){
			vo.setResult(StaticKeys.OH_SUCCESS);
		}else{
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("病人不在院");
		}
		return vo;
	}
	public ModelVo checkPatientOutHospitalAdvice(Map map)throws Exception{
		ModelVo vo = new ModelVo();
		if(hosPatientDao.checkPatientOutHospitalAdvice(map)>0){
			vo.setResult(StaticKeys.OH_SUCCESS);
//			vo.setResultMsg("病人有出院医嘱");
		}else{
			vo.setResult(StaticKeys.OH_FALSE);
		}
		return vo;
	}
	public ModelVo checkPatientChangedOffice(Map map)throws Exception{
		ModelVo vo = new ModelVo();
		if(hosPatientDao.checkPatientChangedOffice(map)>0){
			vo.setResult(StaticKeys.OH_SUCCESS);
			vo.setResultMsg("病人有转科医嘱");
		}else{
			vo.setResult(StaticKeys.OH_FALSE);
		}
		return vo;
	}
	public ModelVo getPatientState(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.getPatientState(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo getPatientComputerInfo(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.getPatientComputerInfo(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	
	public ModelVo getPatientChildren(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.getPatientChildren(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo getPatientOffice(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.getPatientOffice(map);
		
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo checkPatientMedicare(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.checkPatientMedicare(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo checkPatientMedicineAllergy(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.checkPatientMedicineAllergy(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo checkOneCardPatient(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.checkOneCardPatient(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo  patientInfo(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.patientInfo(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo printPagePatientInfo(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.printPagePatientInfo(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo patientAllergyInfo(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.patientAllergyInfo(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo diseaseCatalog()throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.diseaseCatalog();
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo certificateType()throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.certificateType();
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo queryPatientChildren(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.queryPatientChildren(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo checkChangeOffice(long VAF06)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.checkChangeOffice(VAF06);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	private Map getParams(Map map)throws Exception{
		return hosPatientDao.getParams(map);
	}
	private Map getParams2(Map map)throws Exception{
		return hosPatientDao.getParams2(map);
	}
	private boolean params1(Map map)throws Exception{
		return hosPatientDao.params1(map)>0;
	}
	private boolean params2(Map map)throws Exception{
		return hosPatientDao.params2(map)>0;
		}
	private boolean params3(Map map)throws Exception{
		return hosPatientDao.params3(map)>0;
	}
	private boolean params4(Map map)throws Exception{
		return hosPatientDao.params4(map)>0;
	}
	private boolean params5(Map map)throws Exception{
		return hosPatientDao.params5(map)>0;
	}
	private String param204()throws Exception{
		return hosPatientDao.param204();
	}
	//登记附表
	private boolean params6(Map map)throws Exception{
		return hosPatientDao.params6(map)>0;
	}
	//登记附表扩展表
	private boolean params7(Map map)throws Exception{
		return hosPatientDao.params7(map)>0;
	}
	private boolean params8(Map map)throws Exception{
		return hosPatientDao.params8(map)>0;
	}
	public ModelVo execNursePatInputDept(Map map)throws Exception{
		 ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		 String lXml = map.get("lXml").toString();
		 Object lXmlBed = map.get("lXmlBed");
		 Map VAE1= JsonUtil.parserToMap(JSONObject.fromObject(lXml));//病人
		 List<Map<String,Object>> BCQ1 = new ArrayList<>();
		 if(lXmlBed!=null&&!"".equals(lXmlBed)){
			 BCQ1 = JsonUtil.parserToList(JSONArray.fromObject(lXmlBed));//床位
			 for (Map<String,Object> obj : BCQ1) {
				obj.put("VBO01", getNewId.nextId());
			}
		 }
		 
		 map.putAll(VAE1);
		 map.put("TmpBed",BCQ1);
//		 List ls = hosPatientDao.getTempXml(map);
		  Object obj =VAE1.get("IFBed");
//		  Object VAE20Obj= map.get("VAE20");
		  int lBedAll=0;
		  if(obj!=null){
			  lBedAll = Integer.parseInt(obj.toString());
		  }
		  String lDate = DateUtils.getDateString(new Date());
//		  Long lVAAID =Long.parseLong(VAE1.get("VAA01").toString());
		  int lSign = Integer.parseInt(map.get("lSign").toString());
		  int lVAE44 =Integer.parseInt(VAE1.get("VAE44").toString());
		  long lBCKid  =Long.parseLong(map.get("lBCKid").toString());
		  long VAE01 = Long.parseLong(VAE1.get("VAE01").toString());
		  Map map1 = getParams(map);
		  String lvae11 = "0";
		  if(map1!=null&&map1.get("lvae11")!=null){
			  lvae11= map1.get("lvae11").toString();
		  }
		  String VCF11="01";
		  String param204 = param204();
		  int blpsign=0;
		  if(lVAE44==3){
			  VCF11="04";
		  }
		  map.put("lDate", lDate);
		  map.put("vcf11", VCF11);
		  if(map1!=null){
		   map.putAll(map1);
		  }
		  if(lVAE44==1&&params1(map)){
			  vo.setResult(StaticKeys.OH_FALSE);
			  vo.setResultMsg("病人已经撤销入科或已经办理了入科，请刷新后在办理");
			  return vo;
		  }
		  if(lVAE44==3&&params2(map)){
			  vo.setResult(StaticKeys.OH_FALSE);
			  vo.setResultMsg("病人已经撤销转科或已经办理了入科，请刷新后在办理");
			  return vo;
		  }
		  if((lBCKid>0&&lVAE44==1)&&params3(map)){
			  vo.setResult(StaticKeys.OH_FALSE);
			  vo.setResultMsg("当前床位已被占用，请选择别的床位");
			  return vo;
		  }
		  if(lSign == 1&&params4(map)){
			  vo.setResult(StaticKeys.OH_FALSE);
			  vo.setResultMsg("入院时间不能大于记账时间");
			  return vo;
		  }
		  if(lVAE44==1){
			  if(params5(map1)){
				Map map2=getParams2(map);
				map.putAll(map2);
				blpsign=1;
			  }
		  }
		  //修改病人登记记录 、病人资料
		  hosPatientDao.updateVAA2(map);
		  //病人登录附表
		  updateVAM1(map);
		 //病人登记附表扩展表
		  updateVCT1(map);
		  //病人余额表
		  if (lVAE44==1 && blpsign==1){
			  if(params8(map)){
				  hosPatientDao.updateVBM2(map);
			  }else{
				  map.put("lVBM01", getNewId.nextId());
				  hosPatientDao.insertVBM2(map);
			  }
		  }
		  //修改病床表
		  hosPatientDao.updateBCQ1(map);
		  //护理表
		  if(param204.equals("1")){
			  
			  updateVCF2(map);
		  }
		  //病人变动记录
		  if(lVAE44==1){
			  //修改入院的结束时间、原因
			  updateVBO1(map);
			  //转科表
			  updateVBP1(map);
			  //病床表
			  if(lBedAll==1&&lBCKid==0){
				  updateBCQ1_VBO1(map);
			  }
			  //自动记账
			  if("1".equals(map.get("lPara").toString())){
				  
				  HosPatientBo.execClinicVAJ1AutoInsert(lvae11, 1, VAE01, 0);
			  }
		  }
		  if(lVAE44==3){
			  //修改入院的结束时间、原因  转科入科
			  updateVBO1_VBP1(map);
			  if(lBedAll==1){
				  updateBCQ1VBO1(map);
				  
			  }
		  }
		 return vo;
	}
	public void updateBCQ1VBO1(Map map)throws Exception{
		map.put("lID", getNewId.nextId());
		hosPatientDao.updateBCQ1VBO1(map);
	}
	public void updateVBO1_VBP1(Map map)throws Exception{
		hosPatientDao.updateVBO1_VBP1(map);
	}
	public void updateVCF2(Map map)throws Exception{
		map.put("VCF01", getNewId.nextId());
		hosPatientDao.updateVCF2(map);
	}
	public void updateVBO1(Map map)throws Exception{
		map.put("lID", getNewId.nextId());
		hosPatientDao.updateVBO1(map);
	}
	public void updateVBP1(Map map)throws Exception{
		map.put("lID", getNewId.nextId());
		hosPatientDao.updateVBP1(map);
	}
	public void updateBCQ1_VBO1(Map map)throws Exception{
		map.put("lID", getNewId.nextId());
		hosPatientDao.updateBCQ1_VBO1(map);
	}
	private void updateVAM1(Map map)throws Exception{
		if(params6(map)){
			hosPatientDao.updateVAM1(map);
			
		}else{
			hosPatientDao.insertVAM1(map);
		}
	}
	private void updateVCT1(Map map)throws Exception{
		if(params7(map)){
			hosPatientDao.updateVCT1(map);
			
		}else{
			hosPatientDao.insertVCT1(map);
		}
	}
	public ModelVo execNursePatOutDept(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		int lSign = Integer.parseInt(map.get("lSign").toString());
		map.put("lDate", DateUtils.getDateString(new Date()));
		Map map1=getParamsValues(map);
		map.putAll(map1);
		
		if(14==lSign&&checkParams1(map)){
			
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("病人已经撤销出科或已经办理了入科，请刷新后在办理");
			return vo;
		}
		        if(1==lSign&&checkParams2(map)){
					
					vo.setResult(StaticKeys.OH_FALSE);
					vo.setResultMsg("病人已经撤销入科或已经办理了转科，请刷新后在办理");
					return vo;
				}
		if(14==lSign&&checkParams3(map)){
			
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("床位已被占用，请重新选择");
			return vo;
		}
		
		if(1==lSign){
			//修改病人状态 3 转科 修改床位  新增病人变动记录  修改医嘱抄送状态
			updateVAE2(map);
			
		}
		if(14==lSign){
			Map map2=getParamsValues2(map);
			map.putAll(map2);
			updateVAE2_VAM1_BCQ1_VBO1(map);
		}
		return vo;
	}
	
	private boolean checkParams1(Map map)throws Exception{
		return hosPatientDao.checkParams1(map)>0;
	}
	private boolean checkParams2(Map map)throws Exception{
		return hosPatientDao.checkParams2(map)>0;
		}
	private boolean checkParams3(Map map)throws Exception{
		return hosPatientDao.checkParams3(map)>0;
	}
	private Map getParamsValues(Map map)throws Exception{
		return hosPatientDao.getParamsValues(map);
	}
	private void updateVAE2(Map map)throws Exception{
		map.put("lID", getNewId.nextId());
		hosPatientDao.updateVAE2(map);
	}
	private Map getParamsValues2(Map map)throws Exception{
		return hosPatientDao.getParamsValues2(map);
	}
	private void updateVAE2_VAM1_BCQ1_VBO1(Map map)throws Exception{
		map.put("lID", getNewId.nextId());
		map.put("lID1", getNewId.nextId());
		hosPatientDao.updateVAE2_VAM1_BCQ1_VBO1(map);
	}
	public ModelVo checkBedUsed(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		try {
			boolean bool=hosPatientDao.checkBedUsed(map)>0;
			if(bool){
				mo.setResult(StaticKeys.OH_SUCCESS);
			}else{
				mo.setResult(StaticKeys.OH_FALSE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		
		return mo;
	}
	public ModelVo execNursePatChangedBed(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		Object lXmlBed_obj = map.get("lXmlBed");
		List bed = new ArrayList<>();
		if(lXmlBed_obj!=null){
			String lXmlBed = map.get("lXmlBed").toString();
		    bed=JsonUtil.parserToList(JSONArray.fromObject(lXmlBed));
			
		}
		map.put("lDate", DateUtils.getDateString(new Date()));
		map.put("bed",bed);
		map.put("aDate", DateUtils.getDateString(new Date()));
		Map map1 = getParameters(map);
		if(map1!=null){
		  map.putAll(map1);
		}
		if("1".equals(map.get("lSign").toString())&&checkParams4(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("当前床位已被占用，请选择别的床位");
			return vo;
		}
		if(checkParams5(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("病人已经预出院或出院");
			return vo;
		}
		map.put("lID", getNewId.nextId());
		map.put("lID1", getNewId.nextId());
		hosPatientDao.nursePatChangedBed(map);
		
		return vo;
	}
	private Map getParameters(Map map)throws Exception{
		return hosPatientDao.getParameters(map);
	}
	private boolean checkParams4(Map map)throws Exception{
		return hosPatientDao.checkParams4(map)>0;
		}
	private boolean checkParams5(Map map)throws Exception{
		return hosPatientDao.checkParams5(map)==0;
	}
	
	public ModelVo execSysParametersUpdate(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		int state = Integer.parseInt(map.get("State").toString());
		int privilege = Integer.parseInt(map.get("Privilege").toString());
		long UserId = Long.parseLong(map.get("UserId").toString());
		long HostID = Long.parseLong(map.get("HostID").toString());
		String EmpName="",IP ="",Host="";
		int MessageID=0;
		String Event ="",ProgramName ="";
		if(state==0){
			Map map1 = getSysParamters(map);
			if(map1!=null&&map1.size()>0){
				Object _ID=map1.get("id");
				Object _Privilege=map1.get("privilege");
				Object _Value=map1.get("value");
				if(_ID!=null){
					map.put("ID", _ID);
				}else{
					map.put("ID", 0);
				}
				if(_Privilege!=null){
					map.put("Privilege", _Privilege);
					privilege = Integer.parseInt(_Privilege.toString());
				}else{
					map.put("Privilege", 0);
					privilege=0;
				}
				if(_Value!=null){
					map.put("Value", _Value);
				}else{
					map.put("Value", "0");
				}
			}
			if(privilege==0){
				vo.setResult(StaticKeys.OH_FALSE);
				return vo;
			}
			String val = getSysValue(map);
			map.put("Value", val);
			
			
		}
		if(state==1){
			Map map1 = getSysParamters1(map);
			if(map1!=null){
				Object _ID=map1.get("id");
				Object _Privilege=map1.get("privilege");
					map.put("ID", _ID);
					map.put("Privilege", _Privilege);
				if(_Privilege!=null){
					privilege = Integer.parseInt(_Privilege.toString());
				}else{
					privilege=0;
				}
			}
			if(null==map.get("ID")){
				insertSys_param(map);
				
			}
			if(privilege==0){
				if(getSysParamters2(map)){
				if(UserId>0){
				 Map map2 = getEmpName(map);
				 if(map2!=null){
					 EmpName = map2.get("empname").toString();
					 Host= map2.get("host").toString();
				 }
				}
				if(HostID>0){
					 Map map2 = getHost(map);
					 if(map2!=null){
						 EmpName = map2.get("empname").toString();
						 Host= map2.get("host").toString();
						 
						 Event = map2.get("event").toString();
						 ProgramName= map2.get("programname").toString();
					 }
				}
				}
				HashMap hs = new HashMap();
				hs.put("EmpName", EmpName);
				hs.put("IP", IP);
				hs.put("Host", Host);
				hs.put("MessageID", 0);
				hs.put("Event", Event);
				hs.put("ProgramName", ProgramName);
				SYS_EventLogs_Insert(hs);
			}else{
				if(privilege==2){
					UserId=Integer.parseInt(map.get("HostID").toString());
				}
				if(privilege==3){
					UserId=Integer.parseInt(map.get("DeptID").toString());
				}
				map.put("UserId", UserId);
				String ID = getID(map);
				if(null == ID){
					insertSysParam(map);
				}else{
					hosPatientDao.updateSysParam(map);
				}
			}
		}
		
		
		
		
		return vo;
		
	}
	private String getID(Map map)throws Exception{
		return hosPatientDao.getID(map);
	}
	private void insertSysParam(Map map)throws Exception{
		map.put("UserPId", getNewId.nextId());
		hosPatientDao.insertSysParam(map);
	}
	private void SYS_EventLogs_Insert(Map map)throws Exception{
		map.put("EventLogID", getNewId.nextId());
		hosPatientDao.SYS_EventLogs_Insert(map);
	}
	private Map getEmpName(Map map)throws Exception{
		return hosPatientDao.getEmpName(map);
	}
	private Map getHost(Map map)throws Exception{
		return hosPatientDao.getHost(map);
	}
	private Map getSysParamters(Map map)throws Exception{
		return hosPatientDao.getSysParamters(map);
	}
	private Map getSysParamters1(Map map)throws Exception{
		return hosPatientDao.getSysParamters1(map);
	}
	private boolean getSysParamters2(Map map)throws Exception{
		return hosPatientDao.getSysParamters2(map)>0;
	}
	private String getSysValue(Map map)throws Exception{
		return hosPatientDao.getSysValue(map);
	}
	private void insertSys_param(Map map)throws Exception{
		map.put("ID", getNewId.nextId());
		hosPatientDao.insertSys_param(map);
	}
	private boolean checkVAE1(Map map)throws Exception{
		return hosPatientDao.checkVAE1(map)>0;
	}
	private boolean checkVAE2(Map map)throws Exception{
		return hosPatientDao.checkVAE2(map)>0;
	}
	private Map getParams_ABV01_VAA01(Map map)throws Exception{
		return hosPatientDao.getParams_ABV01_VAA01(map);
	}
	public ModelVo execNursePatStopHp(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		if(!checkVAE1(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("病人已经预出院或出院");
			return vo;
		}
		// declare @lZAE01 int,@
		String ABV01="";
		long VAA01=0;
		String ABV01A="";
		String ZAE07 ="";
		Map map1 = getParams_ABV01_VAA01(map);
		if(map1!=null){
			VAA01 = Long.parseLong(map1.get("vaa01")==null?"0":map1.get("vaa01").toString());
			ABV01A = map1.get("abv01a").toString();
		}
		 if (ABV01A.equals("99")){
	       ZAE07 = "取消暂时离院"; 
		 }
		 else{
		   ZAE07 = "改为暂时离院"; 
			 
		 }
		 if(checkVAE2(map1)){
			 ABV01 = "99";
		 }else{
			 ABV01 = "";
		 }
		 map.put("VAA01", VAA01);
		 map.put("ABV01A", ABV01A);
		 map.put("ZAE07", ZAE07);
		 map.put("ABV01", ABV01);
		 updateVAE1_ZAE1(map);
		 
		return vo;
	}
	private void updateVAE1_ZAE1(Map map)throws Exception{
		map.put("lZAE01", getNewId.nextId());
		hosPatientDao.updateVAE1_ZAE1(map);
	}
	public ModelVo queryBaby(Map map) throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.queryBaby(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo checkBaby(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.checkBaby(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
		
	}
	public ModelVo updateBabyOutDept(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		try {
			hosPatientDao.updateBabyOutDept(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setResult(StaticKeys.OH_SUCCESS);
		return mo;
	}
	private boolean checkVAF1(Map map)throws Exception{
		return hosPatientDao.checkVAF1(map)>0;
	}
		private boolean checkVAJ1(Map map)throws Exception{
			return hosPatientDao.checkVAJ1(map)>0;
			}
		private boolean checkVAP1(Map map)throws Exception{
			return hosPatientDao.checkVAP1(map)>0;
		}
	public ModelVo nurseVAP1Del(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		if(checkVAF1(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("此婴儿登记信息已经下达医嘱，不能删除");
			return vo;
		}
		if(checkVAJ1(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("此婴儿登记信息已经对应有费用信息，不能删除");
			return vo;
		}
		if(checkVAP1(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("此婴儿登记信息对应大人已经出院，不能删除");
			return vo;
		}
		VAP1Del(map);
		
		return vo;
	}
	private  void VAP1Del(Map map)throws Exception{
		 map.put("ZAE01", getNewId.nextId());
		 Map m = hosPatientDao.getVAP1_Name(map);
		 map.putAll(m);
		 hosPatientDao.VAP1Del(map);
	}
	private boolean checkVAP07(Map map)throws Exception{
		return hosPatientDao.checkVAP07(map)>0;
	}
   private boolean checkVAP39(Map map)throws Exception{
	   return hosPatientDao.checkVAP39(map)>0;
	}
   private String param206()throws Exception{
	   return hosPatientDao.param206();
   }
   private int getRowr(Map map)throws Exception{
	   return hosPatientDao.getRowr(map);
   }
	public ModelVo execNurseVAP1update(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		String lxml= map.get("lxml").toString();
		List<Map<String,Object>> vap1 = JsonUtil.getList(lxml);
		
		 map.put("adate", DateUtils.getDateString(new Date()));
		  String param206=param206();
//		  if(checkVAP07(map)){
//				vo.setResult(StaticKeys.OH_FALSE);
//				vo.setResultMsg("出生时间输入不正确");
//				return vo;
//			}
//			if(checkVAP39(map)){
//				vo.setResult(StaticKeys.OH_FALSE);
//				vo.setResultMsg("请填写出生地分类（其他）对应名称");
//				return vo;
//			}
			List<Map<String,Object>> saveList=new ArrayList<>();
			List<Map<String,Object>> updateList=new ArrayList<>();
	    if(vap1.size()>0){
	    	for (Map<String,Object> m : vap1) {
				long vap01 = MapUtils.getLongValue(m,"vap01",0l);
				int vap34 = MapUtils.getIntValue(m,"vap34",0);
				m.put("vap34",vap34);
				int vap29 = MapUtils.getIntValue(m,"vap29",0);
				m.put("vap29",vap29);
				if(vap01==0l){
					m.put("vap01", getNewId.nextId());
					saveList.add(m);
				}else{
					updateList.add(m);
				}
	    		
			}
	    }
	    if(saveList.size()>0){//新增记录
	    	map.put("VAP1", saveList);
	    	for (Map<String,Object> m : saveList) {
	    		m.put("rownr", getRowr(map));
	    	}
	    	//添加婴儿
			insertVAP1(map);
			Object VAA07 =vap1.get(0).get("vaa07");
			if("1".equals(param206)&&(VAA07!=null&&!"".equals(VAA07)&&Long.parseLong(VAA07.toString())>0)){
				//新增护理记录
				inserVCF1(map);
				map.put("VAA07", VAA07);
				if(checkVCF11(map)){
					inserVCF2(map);
				}
			}
	    }
	    if(updateList.size()>0){//更新
	    	map.put("VAP1", updateList);
	    	for (Map<String,Object> m : updateList) {
	    		m.put("rownr", getRowr(map));
	    	}
	    	//更新婴儿记录
			updateVCF1(map);
	    }

		return vo;
	}
	private void insertVAP1(Map map)throws Exception{
		hosPatientDao.insertVAP1(map);
	}
	private void inserVCF1(Map map)throws Exception{
		map.put("VCF01", getNewId.nextId());
		hosPatientDao.inserVCF1(map);
	}
	private void inserVCF2(Map map)throws Exception{
		map.put("VCF01", getNewId.nextId());
		hosPatientDao.inserVCF2(map);
	}
	private boolean checkVCF11(Map map)throws Exception{
		return hosPatientDao.checkVCF11(map)==0;
	}
	private void updateVCF1(Map map)throws Exception{
		hosPatientDao.updateVCF1(map);
	}
	private boolean checkBCQ1(Map map)throws Exception{
		return hosPatientDao.checkBCQ1(map)==0;
	}
	private boolean checkBCQ2(Map map)throws Exception{
		return hosPatientDao.checkBCQ1(map)>0;
	}
	private void updateBCQ2(Map map)throws Exception{
		hosPatientDao.updateBCQ2(map);
	}
	public ModelVo execNurseBCQ1Update(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		//床位被借 无人使用
		if(checkBCQ1(map)&&checkBCQ2(map)){
			//修改病床状态
			updateBCQ2(map);
		}
		return vo;
	}
	public ModelVo queryAlarmValue(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.queryAlarmValue(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	private void updateVAA2(Map map)throws Exception{
		hosPatientDao.updateVAA2_BEP05_BEP06(map);
	}
	private void insertVBW1(Map map)throws Exception{
		map.put("VBW01", getNewId.nextId());
		hosPatientDao.insertVBW1(map);
	}
	private Integer getVBM11(Map map)throws Exception{
		return hosPatientDao.getVBM11(map);
	}
	public ModelVo execHOpatientBEP1Update(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		map.put("VAA54A", MapUtils.getInteger(map,"VAA54A",0));	//原担保额度
		map.put("VAA54B", MapUtils.getInteger(map,"VAA54B",0));	//新担保额度
		map.put("BEP05A", MapUtils.getInteger(map,"BEP05A",0));	//原报警值
		map.put("BEP07A", MapUtils.getInteger(map,"BEP07A",0));	//原限制总额

		Integer bep05a = MapUtils.getInteger(map,"BEP05A",0);	//原报警值
		Integer bep05b = MapUtils.getInteger(map,"BEP05B",0);	//新报警值
		Integer bep06a = MapUtils.getInteger(map,"BEP06A",0);	//原信用额度
		Integer bep06b = MapUtils.getInteger(map,"BEP05B",0);	//新信用额度
		Integer bep07a = MapUtils.getInteger(map,"BEP07A",0);	//原限制总额
		Integer bep07b = MapUtils.getInteger(map,"BEP07B",0);	//新限制总额
		Integer acf01 = MapUtils.getInteger(map,"ACF01",2);
		Long vaa01 = MapUtils.getLong(map,"VAA01",0l);
		Long vaa07 = MapUtils.getLong(map,"VAA07",0l);
		String vbw10 = MapUtils.getString(map,"VBW10","");	//担保人
		if((bep05a!=bep05b)||(bep06a!=bep06b)||!"".equals(vbw10)){
			if(acf01==2){
				updateVAA2(map);
			}
		}
		insertVBW1(map);
		//更新费用总额
		if(acf01==2&&bep07a!=bep07b){
			Integer VBM11=getVBM11(map);
			HashMap hs = new HashMap();
			hs.put("VBM01", 0);
			hs.put("VAA01", vaa01);
			hs.put("VBM03", 2);
			hs.put("VBM04", 0);
			hs.put("VBM05", 0);
			hs.put("VBM06", 0);
			hs.put("VAA07",vaa07);
			hs.put("BEP07", bep07b);
			hs.put("VBM11", VBM11==null?0:VBM11);
			execHORateVBM1Update(hs);
		}
		return vo;
	}
	private Map getVBM01_VBM01A_VBM01B(Map map)throws Exception{
		return hosPatientDao.getVBM01_VBM01A_VBM01B(map);
	}
	//HORate_VBM1_Update 更新病人余额表
//	  @VBM01 int out,
//	  @VAA01 int, --病人ID
//	  @VBM03 smallint,--类型(1=门诊、2=住院、4=体检)
//	  @VBM04 numeric(18,4),--本次押金总额
//	  @VBM05 numeric(18,4),--本次费用总额
//	  @VBM06 numeric(18,4),--多次费用总额
//	  @VAA07 int = 0 ,--就诊ID
//	  @BEP07 numeric(18,4) = 0,--限制费用总额
//	  @VBM11 numeric(18,4) = 0--补充费用总额
	public void execHORateVBM1Update(Map map)throws Exception{
		Map map1 = getVBM01_VBM01A_VBM01B(map);
		if(map1!=null&&map1.size()>0){
			Integer vbm03 = MapUtils.getInteger(map,"VBM03",0);
			Long vbm01 =  MapUtils.getLong(map1,"vbm01",0l);
			Long vbm01a =  MapUtils.getLong(map1,"vbm01a",0l);
			Long vbm01b =  MapUtils.getLong(map1,"vbm01b",0l);
			map.put("VBM01",vbm01);
			map.put("VBM01A",vbm01a);
			map.put("VBM01B",vbm01b);
			if(vbm03!=4){
				if(vbm01==0l){//新增余额VBM1
					map.put("VBM01", getNewId.nextId());
					insert_VBM1(map);
				}else{//修改余额VBM1
					update_VBM1(map);
				}

				if(vbm01a==0){
					map.put("VBM01A", getNewId.nextId());
					insert_VBM2(map);
				}else{
					update_VBM2(map);
				}
			}else if(vbm03==4){
				if(vbm01b==0){
					map.put("VBM01A", getNewId.nextId());
					insert_VBM4(map);
				}else{
					update_VBM4(map);
				}

			}
		}
//		long VBM01A=0l;
//        long VBM01B=0l;
//        long VBM01=0l;
//        int VBM03 =0;
//		if(map1!=null&&map1.size()>0){
//			Object VBM01_obj = map1.get("VBM01");
//			Object VBM01A_obj = map1.get("VBM01A");
//			Object VBM01B_obj = map1.get("VBM01B");
//			if(VBM01_obj!=null){
//				VBM01=Long.parseLong(VBM01_obj.toString());
//			}
//			if(VBM01A_obj!=null){
//				VBM01A=Long.parseLong(VBM01A_obj.toString());
//			}
//			if(VBM01B_obj!=null){
//				VBM01B=Long.parseLong(VBM01B_obj.toString());
//			}
//		}
//		if(map.get("VBM03")!=null){
//			VBM03 = Integer.parseInt(map.get("VBM03").toString());
//		}
//		if(VBM03!=4){
//			if(VBM01==0){//新增余额
//
//				insert_VBM1(map);
//			}else{//修改余额
//				map.put("VBM01A", VBM01A);
//				update_VBM1(map);
//
//			}
//			if(VBM01A==0){
//				insert_VBM2(map);
//			}else{
//				map.put("VBM01A", VBM01A);
//				update_VBM2(map);
//			}
//
//		}else if(VBM03==4){
//			if(VBM01B==0){
//				insert_VBM4(map);
//			}else{
//				map.put("VBM01B", VBM01A);
//				update_VBM4(map);
//			}
//
//		}
	}
	private void insert_VBM1(Map map)throws Exception{

		hosPatientDao.insert_VBM1(map);
	}
	private void update_VBM1(Map map)throws Exception{
		hosPatientDao.update_VBM1(map);
	}
	private void insert_VBM2(Map map)throws Exception{
		hosPatientDao.insert_VBM2(map);
	}
	private void update_VBM2(Map map)throws Exception{
		hosPatientDao.update_VBM2(map);
	}
	private void insert_VBM4(Map map)throws Exception{

		hosPatientDao.insert_VBM4(map);
	}
	private void update_VBM4(Map map)throws Exception{
		hosPatientDao.update_VBM4(map);
	}
	
	//判断病人
	private boolean checkState1(Map map)throws Exception{
		
		return hosPatientDao.checkState1(map)>0;
	}
	private boolean checkState2(Map map)throws Exception{
		return hosPatientDao.checkState2(map)==0;
			
		}
	private boolean checkState3(Map map)throws Exception{
		return hosPatientDao.checkState3(map)==0;
		
	}
	private void update_BCQ1_VBO1(Map map)throws Exception{
		map.put("lID", getNewId.nextId());
		hosPatientDao.update_BCQ1_VBO1(map);
	}
	public ModelVo nursePatOutAllBed(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		int lType = Integer.parseInt(map.get("lType").toString());
		Boolean isExists = checkState1(map);
		if(isExists){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("当前病人已不是在院状态，不能进行操作");
			return vo;
		}
		if(lType==0&&checkState2(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("当前病人没有包床");
			return vo;
		}
		if(lType==1&&checkState3(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("当前病人没有包房");
			return vo;
		}
	    String lDate = DateUtils.getDateString(new Date());
	    map.put("lDate", lDate);
	    update_BCQ1_VBO1(map);
		
		
		return vo;
		
	}
	public ModelVo patientAutoComplete(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.patientAutoComplete(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo queryBondsMan()throws Exception{
		
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.queryBondsMan();
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
		
	}
	public ModelVo queryChildren(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.queryChildren(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	private boolean checkPatientOut(Map map)throws Exception{
		return hosPatientDao.checkPatientOut(map)>0;
	}
	private boolean checkPay(Map map)throws Exception{
		return hosPatientDao.checkPay(map)>0;
	}
	private boolean checkBed(Map map)throws Exception{
		return hosPatientDao.checkBed(map)==0;
	}
	private Map sysParaValue()throws Exception{
		return hosPatientDao.sysParaValue();
	}
	private boolean isAudit(Map map)throws Exception{
		return hosPatientDao.isAudit(map)>0;
		
	}
    private boolean isBackUp(Map map)throws Exception{
		return hosPatientDao.isBackUp(map)>0;
		
	}
	public ModelVo execNursePatComeBackHosp(Map map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		if(checkPatientOut(map)){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("此病人已经办理出院手续，不能在召回预出院");
			return mo;
		}
		if(checkPay(map)){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("此病人已经办理医保结帐，不能在召回预出院");
			return mo;
		}
		if(checkBed(map)){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("床位已占用，请选择别的床位");
			return mo;
		}
		Map m = sysParaValue();
		if("1".equals(m.get("param16").toString())&&isAudit(map)){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("已办理出院审核，不能撤销预出院，请先取消审核");
			return mo;
		}
		if("1".equals(m.get("param252").toString())&&isBackUp(map)){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("首页已归档，不允许撤销预出院");
			return mo;
		}
		Map m1 = hosPatientDao.getBCQ04(map);
		map.putAll(m1);
		map.put("vbg01", getNewId.nextId());
		map.put("vbo01", getNewId.nextId());
		map.put("lFDate", DateUtils.getDateString(new Date()));
		//修改病人状态、 床位号、 删除诊断信息、病人入院时间、修改转科记录、删除出院医嘱
		hosPatientDao.updateVAA1_BCQ1_VBO1(map);
		return mo;
		
	}
	public ModelVo execBackOutDept(Map map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		hosPatientDao.backOutDept(map);
		return mo;
	}
	public ModelVo checkBlance(long vaa01)throws Exception{
		ModelVo mo = new ModelVo();
		int count = hosPatientDao.checkBlance(vaa01);
		if(count>0){
			mo.setResult(StaticKeys.OH_SUCCESS);
		}else{
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("病人还有费用不能撤销入科");
		}
		return mo;
	}
	public ModelVo querypatientDetail(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.querypatientDetail(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	
	public ModelVo execHoPatientVAE1Update(Map map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		if(hosPatientDao.isPatientOut(map)>0){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("病人已经出院，不允许修改登记信息");
			return mo;
		}
		if(hosPatientDao.checkVAA1_ID_4()==0){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("住院号序列号表为空，不能生成住院号");
			return mo;
		}
		
		Map<String,Object> patientInfo = JsonUtil.getMap(map.get("Xml").toString());
		map.put("VAE1", patientInfo);
		if(hosPatientDao.paramNo()>0){
			if(hosPatientDao.checkBDP02(map)==0){
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("病人类别与费别不对应");
				return mo;
			}
		}
		if(patientInfo.get("VAA05")==null||"".equals(patientInfo.get("VAA05").toString())){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("病人姓名不能为空");
			return mo;
		}
		if(patientInfo.get("VAA04")==null||"".equals(patientInfo.get("VAA04").toString())){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("病人住院号不能为空");
			return mo;
		}
		//住院次数是否存在
		if(hosPatientDao.isVAE19(map)>0){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("已存在住院记录，请修改住院次数后重新保存");
			return mo;
		}
		if(patientInfo.get("VAE19")==null||Integer.parseInt(patientInfo.get("VAE19").toString())<=0){
			Integer vae19 = hosPatientDao.getVAE19(map);
			patientInfo.put("VAE19", vae19);
		}
		if(hosPatientDao.checkParamsNo()>0&&hosPatientDao.checkVAA1(map)>0){
			
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("已绑定会员卡类型，禁止修改病人费别");
			return mo;
		}
		if(hosPatientDao.checkVAA02(map)>0){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("会员卡号已使用");
			return mo;
		}
		//如果住院号为空，自动生成住院号
		if(patientInfo.get("VAA04")==null||"".equals(patientInfo.get("VAA04").toString())){
			String WardNoPre="";
			Map map1 = hosPatientDao.getWardNoLen_WardNoPre();
			int WardNoLen = Integer.parseInt(map1.get("wardnolen").toString());
			String VAA04 ="";
			if(map1!=null){
				if("YYYYMMDD".equals(map1.get("wardnopre").toString())){
					WardNoPre = getSimpleDateFormat("YYYYMMDD");
					
				}else if("YYYY".equals(map1.get("wardnopre").toString())){
					WardNoPre = getSimpleDateFormat("YYYY");
				}else if("YY".equals(map1.get("wardnopre").toString())){
					WardNoPre = getSimpleDateFormat("YY");
					
				}else if("YYYYMM".equals(map1.get("wardnopre").toString())){
					WardNoPre = getSimpleDateFormat("YYYYMM");
				}
				
			}
			//门诊
			if((patientInfo.get("VAE04")!=null &&"1".equals(patientInfo.get("VAE04").toString()))&&hosPatientDao.param117()>0){
				String value=getNewId.nextId()+"";
				VAA04 = WardNoPre+value.substring(WardNoLen -WardNoPre.length());
			}else{//住院
				String value = getVAA04ID()+"";
				VAA04 = WardNoPre+value.substring(WardNoLen -WardNoPre.length());
				map.put("VAA04", VAA04);
				map.put("WardNoLen", WardNoLen);
				map.put("WardNoPre", WardNoPre);
				//住院号存在
				if(hosPatientDao.isVAA04(map)>0){
					int maxValue=hosPatientDao.getMaxValue(map);
					HashMap hs = new HashMap();
					hs.put("MaxValue", maxValue);
					hosPatientDao.update_VAA04ID(hs);
					VAA04 = WardNoPre+String.valueOf(maxValue+1).substring(WardNoLen -WardNoPre.length());
				}
			}
			patientInfo.put("VAA04", VAA04);
			map.put("VAE1", patientInfo);
		}
		if(hosPatientDao.isEmpty_VAA04(map)>0){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("住院号已使用");
			return mo;
		}
		
		Map m1 = hosPatientDao.getOBDP02_OABC02(map);
		Map m2 = hosPatientDao.getVAA04_VAA05(map);
		if(!m2.get("vaa05").toString().equals(m1.get("ovaa05").toString())){
			if(getEmpPermissionValue(104004021,Long.parseLong(map.get("EmpId").toString()))==1){
				if(hosPatientDao.isVAE44(map)>0){
					
					mo.setResult(StaticKeys.OH_FALSE);
					mo.setResultMsg("已结账病人的姓名不允许修改");
					return mo;
				}
			}
		}
		if(!m2.get("vaa04").toString().equals(m1.get("ovaa04").toString())){
			if(getEmpPermissionValue(104004021,Long.parseLong(map.get("EmpId").toString()))==1){
				if(hosPatientDao.isVAE44(map)>0){
					mo.setResult(StaticKeys.OH_FALSE);
					mo.setResultMsg("已结账病人的住院号不允许修改");
					return mo;
				}
			}
		}
		if(map.get("VAA01")==null||"0".equals(map.get("VAA01").toString())){//新增
			map.put("VAA01", getNewId.nextId());
			hosPatientDao.insert_VAA1(map);
			
		}else{
			if(map.get("VAE01")==null||"0".equals(map.get("VAE01").toString())){
				//调整信用值和报警值
				hosPatientDao.updateVAA1_BEP06_BEP05(map);
			}
			//添加姓名变动记录
			if(!m2.get("vaa05").toString().equals(m1.get("ovaa05").toString())){
				map.put("ZAE01", getNewId.nextId());
				map.put("VAA05", m2.get("vaa05").toString());
				map.put("OVAA05", m1.get("ovaa05").toString());
				hosPatientDao.insertZAE1(map);
			}
			//添加住院号记录
			if(!m2.get("vaa04").toString().equals(m1.get("ovaa04").toString())){
				map.put("ZAE01", getNewId.nextId());
				map.put("VAA04", m2.get("vaa04").toString());
				map.put("OVAA04", m1.get("ovaa04").toString());
				hosPatientDao.insertZAE1_VAA04(map);
				
			}
            //费别变动记录
			if(!patientInfo.get("ABC02").toString().equals(m1.get("oabc02").toString())){
				map.put("ZAE01", getNewId.nextId());
				map.put("ABC02", patientInfo.get("ABC02").toString());
				map.put("OABC02", m1.get("oabc02").toString());
				hosPatientDao.insertZAE1_ABC02(map);
			}
			//类别变动记录
			if(!patientInfo.get("BDP02").toString().equals(m1.get("obdp02").toString())){
				map.put("ZAE01", getNewId.nextId());
				map.put("ABC02", patientInfo.get("BDP02").toString());
				map.put("OBDP02", m1.get("obdp02").toString());
				hosPatientDao.insertZAE1_BDP02(map);
			}
			//修改病人信息
			hosPatientDao.update_VAA1(map);
			//修改病人信用额度
			this.update_VAA1_BEP06(map);
			//限制费用总额和药品总额
			updateVBM2(map);
			
			
		}
		//计算病人年龄
		String VAE87 = hosPatientDao.getVAE87(map);
		patientInfo.put("VAE87", VAE87);
		//保存病人登记信息
		if(map.get("VAE01")==null||"".equals(map.get("VAE01").toString())||"0".equals(map.get("VAE01").toString())){
			map.put("VAE01", getNewId.nextId());
			hosPatientDao.add_VAE1(map);
			//更新预交款
			if(patientInfo.get("VBL13")==null&&Float.parseFloat(patientInfo.get("VBL13").toString())>0){
				HashMap hs = new HashMap();
				hs.put("VBL01", 0);
				hs.put("VBL02", patientInfo.get("VBL02").toString());
				hs.put("VBL03","" );
				hs.put("VBL04",4 );
				hs.put("VBL05", 0);
				hs.put("VAA01",Long.parseLong(map.get("VAA01").toString()));
				hs.put("VAA07", Long.parseLong(map.get("VAE01").toString()));
				hs.put("BCK01", Integer.parseInt(map.get("EmpDept").toString()));
				hs.put("BAQ03", patientInfo.get("BAQ03").toString());
				hs.put("BBO02", patientInfo.get("BBO02").toString());
				hs.put("VBL11", patientInfo.get("VBL11").toString());
				hs.put("VBL12", "");
				hs.put("VBL13", patientInfo.get("VBL13").toString());
				hs.put("VBL14", patientInfo.get("VBL14").toString());
				hs.put("VBL18", DateUtils.getDateString(new Date()));
				hs.put("VBL19",DateUtils.getDateString(new Date()));
				hs.put("BCE01", Integer.parseInt(map.get("EmpId").toString()));
				hs.put("BCE02", map.get("EmpCode").toString());
				hs.put("BCE03", map.get("EmpName").toString());
				hs.put("FAA01", patientInfo.get("FAA01").toString());
				hs.put("FAF01", 0);
				hs.put("VBU01", 0);
				
				mo=hoPatient_PrePay_Entry(hs,mo);
				if(!mo.getResult().equals(StaticKeys.OH_SUCCESS)){
					return mo;
				}
			}
			//修改入院通知
			if(patientInfo.get("VAC01")!=null&&Long.parseLong(patientInfo.get("VAC01").toString())>0){
				hosPatientDao.update_VBN1(map);
			}
			//更新病人余额
			  HashMap hs = new HashMap();
				hs.put("VBM01", 0);
				hs.put("VAA01", map.get("VAA01").toString());
				hs.put("VBM03", 2);
				hs.put("VBM04", 0);
				hs.put("VBM05", 0);
				hs.put("VBM06", 0);
				hs.put("VAA07", map.get("VAE01").toString());
				hs.put("BEP07", hosPatientDao.getBEP07(map)==null?0:hosPatientDao.getBEP07(map));
				hs.put("VBM11", patientInfo.get("VBM11")==null?0:patientInfo.get("VBM11").toString());
			  execHORateVBM1Update(hs);
		}else{
			
			String OVAE88 = hosPatientDao.getVAE88(map);
			if(OVAE88!=null&&!OVAE88.equals(patientInfo.get("VAE88").toString())){
				map.put("OVAE88", OVAE88);
				hosPatientDao.saveZAE1(map);
			}
			//如果病人未入院，修改病区和住院科室
			hosPatientDao.modifyBCK01C_BCK01D(map);
			HashMap hs = new HashMap();
			hs.put("VBM01", 0);
			hs.put("VAA01", map.get("VAA01").toString());
			hs.put("VBM03", 2);
			hs.put("VBM04", 0);
			hs.put("VBM05", 0);
			hs.put("VBM06", 0);
			hs.put("VAA07", map.get("VAE01").toString());
			hs.put("BEP07", patientInfo.get("BEP07")==null?0:patientInfo.get("BEP07").toString());
			hs.put("VBM11", patientInfo.get("VBM11")==null?0:patientInfo.get("VBM11").toString());
		    execHORateVBM1Update(hs);
		  
		}
		//修改病人诊断记录
		HOPatient_VAO1_Update(map, patientInfo);
		//病人变动记录
		if("1".equals(map.get("WorkStation").toString())){
			   HashMap hs = new HashMap();
			
			   hs.put("VBO01",patientInfo.get("VBO01").toString() );
			   hs.put("VAA01",map.get("VAA01").toString() );
			   hs.put("VAA07", map.get("VAE01").toString());
			   hs.put("VBO04",patientInfo.get("VAE82").toString() );
			   hs.put("VBO06",1 );
			   hs.put("BCK01A", patientInfo.get("BCK01A").toString());
			   hs.put("BCK01B",patientInfo.get("BCK01B").toString() );
			   hs.put("BBY01A", patientInfo.get("AAG01").toString());
			   hs.put("ABO01",patientInfo.get("ABO01").toString() );
			   hs.put("BCE01", map.get("EmpId").toString());
			   hs.put("BCE03F", map.get("EmpName").toString());
		       HOPatient_Pat_Change(hs);
		}else if("2".equals(map.get("WorkStation").toString())){
			   HashMap hs = new HashMap();
			    hs.put("VBO01",hosPatientDao.getVBO01(map));
			    hs.put("VAA01",map.get("VAA01").toString());
			    hs.put("VAA07",map.get("VAE01").toString());
			    hs.put("VBO04",DateUtils.getDateString(new Date()));
			    hs.put("VBO06",7);
			    hs.put("BCK01A",patientInfo.get("BCK01A").toString());
			    hs.put("BCK01B",patientInfo.get("BCK01B").toString());
			    hs.put("BBY01A",patientInfo.get("AAG01").toString());
			    hs.put("ABO01",patientInfo.get("ABO01").toString());
			    hs.put("BCE03A",patientInfo.get("BCE03B").toString());
			    hs.put("BCE03B",patientInfo.get("BCE03C").toString());
			    hs.put("BCE01",map.get("EmpId").toString());
			    hs.put("BCE03F",map.get("EmpName").toString());
			     HOPatient_Nurse_PatChange(hs);
		}
		if(map.get("DiagId")!=null&&!"0".equals(map.get("DiagId").toString())){
			hosPatientDao.updateVAE08(map);
		}
		//更新CRM
		if(patientInfo.get("SCF01")!=null&&!"0".equals(patientInfo.get("SCF01").toString())){
			hosPatientDao.updateSCF1(map);
			
		}
		//更新CRM咨询师
		if(patientInfo.get("QueryPeopleId")!=null&&!"".equals(patientInfo.get("QueryPeopleId").toString())){
			   HashMap hs = new HashMap();
			    hs.put("SCI01",0);
			    hs.put("SCA01",patientInfo.get("SCA01").toString());
			    hs.put("SCI03",2);
			    hs.put("SCI04",patientInfo.get("SCF01").toString());
			    hs.put("SCI05",0);
			    hs.put("BCE01",patientInfo.get("QueryPeopleId").toString());
			    hs.put("BCE03",patientInfo.get("QueryPeople").toString());
			    hs.put("Xml","");
			    hs.put("IsBatch",0);
			    hs.put("ACF01",3);
			    hs.put("SCI11",0);
			    hs.put("VAA07",map.get("VAE01").toString());
			    hs.put("SCI13",1);
			    hs.put("SCI14", "");
			
			    HOCRM_SCI1_Update(hs);
			
		}
		String SCA01= hosPatientDao.getSCA01(map);
		map.put("SCA01", SCA01);
		if(hosPatientDao.checkSCA1(map)==0){
			hosPatientDao.updateSCA1(map);
		}
		if(SCA01!=null&&Long.parseLong(SCA01)>0){
			if(patientInfo.get("CManageId")!=null&&Long.parseLong(patientInfo.get("CManageId").toString())>0){
			HashMap hs = new HashMap();
			hs.put("SCA01", 0);
			hs.put("BCE01",patientInfo.get("CManageId").toString() );
			hs.put("BCE03", patientInfo.get("CManage").toString());
			hs.put("Status", 0);
			mo=HOCRM_SCA1_Status(hs,mo);
			if(mo.getResult().equals(StaticKeys.OH_FALSE)){
				return mo;
			}
			}
			
			String BCK03B = hosPatientDao.getBCK03B(map);
			String SCH12 = "住院号：" + patientInfo.get("VAA04").toString() + " 入院时间：" + patientInfo.get("VAE11").toString() + " 入院科室：" + BCK03B + " 门诊医生：" + patientInfo.get("BCE03B")==null?"":patientInfo.get("BCE03B").toString();
			HashMap m = new HashMap();
			
			m.put("SCH01", 0);
			m.put("SCA01", patientInfo.get("SCA01").toString());
			m.put("SCH03", 0);
			m.put("SCH06", "VAE1");
			m.put("SCH07", "VAE01");
			m.put("SCH10", 201);
			m.put("SCH11", "入院");
			m.put("SCH12", SCH12);
			m.put("SCH13", 0);
			m.put("SCH14", 0);
			m.put("BCE01", map.get("EmpId").toString());
			m.put("BCE03", map.get("EmpName").toString());
			m.put("SCH16", "");
			m.put("SCH23", null);
			HOCRM_SCH1_Update(m);
			
		}
		//病人登记附表
		if(hosPatientDao.checkVCT1(map)==0){
			hosPatientDao.addVCT1(map);
		}else{
			hosPatientDao.modifyVCT1(map);
		}
		return mo;
	}
	private void HOPatient_VAO1_Update(Map map,Map patientInfo)throws Exception{
		HashMap hs = new HashMap();
		hs.put("VAA01", map.get("VAA01").toString());
		map.put("VAE01", map.get("VAE01").toString());
		hs.put("VAO01", patientInfo.get("VAO01A").toString());
		hs.put("BAK01", patientInfo.get("BAK01A").toString());
		hs.put("ACF01", 2);
		hs.put("VAO06", 1);
		hs.put("VAO07", 1);
		hs.put("VAO10", 0);
		map.put("VAO11", 1);
		map.put("ABX01", null);
		hs.put("VAO18", 0);
		hs.put("EmpName", map.get("EmpName").toString());
		hs.put("VAO15", patientInfo.get("BAK05A").toString());
		hs.put("BAK01B", 0);
		hs.put("CAM01", 0);
		hs.put("VAO22", 0);
		hs.put("VAF01", 0);
		hs.put("VAO24", 0);
		//门诊诊断
		hoschargeBo.execHOPatient_VAO1_Update(hs);
		hs.put("VAO01", patientInfo.get("VAO01B").toString());
		hs.put("BAK01", patientInfo.get("BAK01B").toString());
		hs.put("VAO07", 0);
		hs.put("VAO10", 1);
		hs.put("VAO15", patientInfo.get("BAK05B").toString());
		//中医诊断
		hoschargeBo.execHOPatient_VAO1_Update(hs);
		//入院诊断
		hs.put("VAO01", patientInfo.get("VAO01C").toString());
		hs.put("BAK01", patientInfo.get("BAK01C").toString());
		hs.put("VAO07", 0);
		hs.put("VAO10", 0);
		map.put("VAO11", 2);
		hs.put("VAO15", patientInfo.get("BAK05C").toString());
		hoschargeBo.execHOPatient_VAO1_Update(hs);
	}
	private void update_VAA1_BEP06(Map map)throws Exception{
		if(hosPatientDao.SYS_Parameters()>0){
			Map m1 = hosPatientDao.get_VBM04_VBM54_BEP06(map);
			Float VBM04 = m1.get("vbm04")==null?0:Float.parseFloat(m1.get("vbm04").toString());
			Float VAA54 = m1.get("vaa54")==null?0:Float.parseFloat(m1.get("vaa54").toString());
			Map m2 = hosPatientDao.get_BEP08_BEP09_BEP06(map);
			Float BEP08 = m2.get("bep08")==null?0:Float.parseFloat(m2.get("bep08").toString());
			Float BEP09 = m2.get("bep09")==null?0:Float.parseFloat(m2.get("bep09").toString());
			Float BEP06 = m2.get("bep06")==null?0:Float.parseFloat(m2.get("bep06").toString());
			if(BEP06==0){
				BEP06 = VBM04*BEP09+VAA54*BEP08;
			}else{
				BEP06 =  VAA54 * BEP08;
			}
			map.put("BEP06", BEP06);
			hosPatientDao.update_VAA1_BEP06(map);
		}
	}
	public void updateVBM2(Map map)throws Exception{
		if(hosPatientDao.checkBLP1(map)>0){
			if(hosPatientDao.checkVBM2(map)>0){
				hosPatientDao.modify_VBM2(map);
			}else{
				map.put("lvbm01", getNewId.nextId());
				hosPatientDao.add_VBM2(map);
			}
		}
	}
	private int getEmpPermissionValue(long ElementID,long BCE01)throws Exception{
		int value=0;
		HashMap hs = new HashMap();
		hs.put("ElementID", ElementID);
		hs.put("BCE01", BCE01);
		if(hosPatientDao.SYS_RolePermissions1(hs)>0||hosPatientDao.SYS_RolePermissions2(hs)>0){
			value=1;
		}
		return value;
	}
	private String getSimpleDateFormat(String pattern)throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date());
	}
	//获取住院序列号
	private long getVAA04ID()throws Exception{
		return hosPatientDao.getVAA04ID();
	}
	public ModelVo checkPatientFee(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.checkPatientFee(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo selectPatientChangeInfo(Map map,int currPage,int pageSize)throws Exception{
		ModelVo mo = new ModelVo();
		Page pg =new Page();
		long start = System.currentTimeMillis();
		try {
			pg=hosPatientDao.selectPatientChangeInfo(map, currPage, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(pg.getList());
		mo.setPage(currPage);
		mo.setRecords(pg.getTotalNumber());
		long end = System.currentTimeMillis();
		log.info("============================selectPatientChangeInfo 时间===========================:"+(start-end));
		return mo;
		
		
	}
	public ModelVo viewPatient(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.viewPatient(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo updateChildrenState(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		try {
			hosPatientDao.updateChildrenState(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setResult(StaticKeys.OH_SUCCESS);
		return mo;
	}
	public  ModelVo queryOutPatient(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientDao.queryOutPatient(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	  public ModelVo querySponsor()throws Exception{
		  ModelVo mo = new ModelVo();
			List pg = null;
			try {
				pg = hosPatientDao.querySponsor();
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			
			mo.setList(pg);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg);
			return mo;
		  
	  }
	  public ModelVo getPatientCout(Map map)throws Exception{
		  ModelVo mo = new ModelVo();
		  int pg=0;
			try {
				pg = hosPatientDao.getPatientCout(map);
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setTotal(pg);
			mo.setResult(StaticKeys.OH_SUCCESS);
			return mo;
	  }
	  //修改病人预交款
	  /**
	   *  @VBL01 int out,         
		    @VBL02 varchar(20),   
		    @VBL03 varchar(20),   
		    @VBL04 tinyint,            
		    @VBL05 tinyint,            
		    @VAA01 int,                
		    @VAA07 int,                
		    @BCK01 int,               
		    @BAQ03 varchar(128),
		    @BBO02 varchar(64),  
		    @VBL11 varchar(20),   
		    @VBL12 varchar(128), 
		    @VBL13 numeric(18,4) ,
		    @VBL14 varchar(32),    
		    @VBL18 datetime,       
		    @VBL19 datetime,       
		    @BCE01 int,                
		    @BCE02 varchar(20),   
		    @BCE03 varchar(20),   
		    @FAA01 int = 0 ,
		    @FAF01 int = 0 ,
		    @VBU01 int = 0 
	   * @param map
	   * @param mo
	   * @return
	   * @throws Exception
	   */
	  public ModelVo hoPatient_PrePay_Entry(Map map,ModelVo mo)throws Exception{
		  String VBL15=hosPatientDao.getVBL15(map);
		  if(hosPatientDao.check_out_hospital(map)>0){
			  mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("当前病人已经出院结算，不允许做预交款操作");
				return mo;
		  }
		  if(hosPatientDao.check_cancel_inhospital(map)>0){
			  mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("当前病人已经取消入院，不允许做预交款操作");
				return mo;
		  }
		  if(VBL15==null||"".equals(VBL15)){
			  mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("支付方式不存在，请检查");
				return mo;
		  }
		  if(hosPatientDao.check_SYS_Parameters()>0&&hosPatientDao.check_VBU1(map)>0){
			  mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("当前住院患者是会员,请在会员管理中进行充值");
				return mo;
		  }
		  if(hosPatientDao.check_VBL18(map)>0){
			  mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("收款时间不能小于病人入院时间");
				return mo;  
		  }
		  if(VBL15!=null&&"05".equals(VBL15)){
			  if(hosPatientDao.check_VBU2(map)==0){
				  
				  mo.setResult(StaticKeys.OH_FALSE);
				  mo.setResultMsg("当前会员卡余额不足或不存在会员账号");
				  return mo;
				  
			  }
		  }
		  long VBL01B =getNewId.nextId();
		  map.put("VBL01", VBL01B);
		  hosPatientDao.modify_VBL1(map);
		  if((VBL15!=null&&"05".equals(VBL15))&&(map.get("VBU01")!=null&&!"".equals(map.get("VBU01").toString())&&Long.parseLong(map.get("VBU01").toString())>0)){
			  map.put("VBL01",getNewId.nextId());
			  hosPatientDao.addVBL1(map);
			  map.put("VCA01", getNewId.nextId());
			  hosPatientDao.update_VBU1_VCA1_VBL1(map);
		  }
		  //修改病人余额
		  HashMap hs = new HashMap();
			hs.put("VBM01", 0);
			hs.put("VAA01", map.get("VAA01").toString());
			hs.put("VBM03", 2);
			hs.put("VBM04", map.get("VBL13").toString());
			hs.put("VBM05", 0);
			hs.put("VBM06", 0);
			hs.put("VAA07", map.get("VAA07").toString());
			hs.put("BEP07", 0);
			hs.put("VBM11", 0);
		  execHORateVBM1Update(hs);
		  HashMap h = new HashMap();
		  h.put("VBM13", map.get("VBM13")==null?0:Float.parseFloat(map.get("VBM13").toString()));
		  h.put("VAA54", 0);
		  h.put("BDP02", getBDP02(map));
		  h.put("BCK01", 0);
		  h.put("IsDef", 0);
		  h.put("VAA01", map.get("VAA01").toString());
		  updateVAA1_BEP06(h);
		  return mo;
	  }
	  private void updateVAA1_BEP06(Map map)throws Exception{
		  Map m2 = hosPatientDao.get_BEP08_BEP09_BEP06(map);
			Float BEP08 = m2.get("bep08")==null?0:Float.parseFloat(m2.get("bep08").toString());
			Float BEP09 = m2.get("bep09")==null?0:Float.parseFloat(m2.get("bep09").toString());
			Float BEP06 = m2.get("bep06")==null?0:Float.parseFloat(m2.get("bep06").toString());
			Float VBM04 =  map.get("VBM13")==null?0:Float.parseFloat(map.get("VBM13").toString());
			Float VAA54 =  map.get("VAA54")==null?0:Float.parseFloat(map.get("VAA54").toString());
			if(BEP06==0){
				BEP06 = VBM04*BEP09+VAA54*BEP08;
			}else{
				BEP06 =  VAA54 * BEP08;
			}
			map.put("BEP06", BEP06);
			hosPatientDao.updateVAA1_BEP06(map);
	  }
	  private String getBDP02(Map map)throws Exception{
		  return hosPatientDao.getBDP02(map);
	  }
	  public void HOPatient_Pat_Change(Map map)throws Exception{
		  if(map.get("VBO01")==null||"0".equals(map.get("VBO01").toString())){
			  map.put("VBO01", getNewId.nextId());
			  hosPatientDao.addVBO1_VBP1(map);
		  }else{
			  hosPatientDao.modifyVBO1_VBP1(map);
		  }
	  }
	  public void HOPatient_Nurse_PatChange(Map map)throws Exception{
		  if(map.get("VBO01")==null||"0".equals(map.get("VBO01").toString())){
			  map.put("VBO01", getNewId.nextId());
			  hosPatientDao.insertVBO1(map);
		  }else{
			  Map m = hosPatientDao.getABO01O(map);
			  String ABO01O = m.get("abo01o")==null?"0":m.get("abo01o").toString();
			  String BCE03AO = m.get("bce03ao")==null?"0":m.get("bce03ao").toString();
			  String BCE03BO = m.get("bce03bo")==null?"0":m.get("bce03bo").toString();
			  if(map.get("ABO01").toString().equals(ABO01O)&&map.get("BCE03A").toString().equals(BCE03AO)&&map.get("BCE03B").toString().equals(BCE03BO)){
				  return ;
			  }
			  map.put("VBO01", getNewId.nextId());
			  hosPatientDao.update_VBO1(map);
		  }
		  
	  }
	  public void HOCRM_SCI1_Update(Map map)throws Exception{
		  if("0".equals(map.get("IsBatch").toString())){
			  if(hosPatientDao.checkSCI1(map)>0){
				  return ;
			  }
			 String SCI01 = hosPatientDao.getSCI01(map);
			 if(SCI01==null||"0".equals(SCI01)){
			     map.put("SCI01", getNewId.nextId());
			     hosPatientDao.addSCI1(map);
			 }else{
				 map.put("SCI01", SCI01);
				 
				 hosPatientDao.updateSCF1(map);
			 }
			  
			  
			  
		  }
		  
	  }
	  public ModelVo HOCRM_SCA1_Status(Map map,ModelVo mo)throws Exception{
		  if("0".equals(map.get("Status").toString())){
			  if(hosPatientDao.getPatnStatus(map)>0){
				  if(hosPatientDao.checkSCA2(map)>0){
					  mo.setResult(StaticKeys.OH_FALSE);
						mo.setResultMsg("当前客户是独占状态，不允许分配客户经理");
						return mo; 
				  }
				  if(hosPatientDao.checkSCA3(map)>0){
					  mo.setResult(StaticKeys.OH_FALSE);
						mo.setResultMsg("当前客户是签约状态，不允许分配客户经理");
						return mo; 
				  }
				  if(hosPatientDao.checkSCP2(map)==0){
					  mo.setResult(StaticKeys.OH_FALSE);
						mo.setResultMsg("当前员工未维护客户资源配额记录");
						return mo; 
				  }
				  if(hosPatientDao.checkSCP3(map)>0){
					  mo.setResult(StaticKeys.OH_FALSE);
						mo.setResultMsg("当前客户经理客户资源配额已满，不允许分配客户");
						return mo; 
				  }
				  if(hosPatientDao.checkSCI2(map)>0){
					  return mo; 
				  }
			  }
			  String SCI01 = hosPatientDao.getSCI1(map);
			  if(SCI01!=null&&Long.parseLong(SCI01)>0){
				  hosPatientDao.modifySCI1(map);
			  }else{
				  map.put("SCI01", getNewId.nextId());
				  hosPatientDao.saveSCI1(map);
				  
			  }
			  hosPatientDao.modifySCA1_SCP1(map);
			  
		  }
		  String OldBCE03 = hosPatientDao.getOldBCE03(map);
		  //客户状态变更，写入记录变动表
	    	HashMap hs = new HashMap();
	    	hs.put("ZAE01", map.get("ZAE01").toString());
	    	hs.put("TNAME", "SCA1");
	    	hs.put("CNAME", "SCA38");
	    	hs.put("ZAE04", map.get("SCA01").toString());
	    	hs.put("ZAE05", map.get("ZAE01").toString());
	    	hs.put("ZAE06",OldBCE03 );
	    	hs.put("ZAE07", map.get("BCE03").toString());
	    	hs.put("ZAE08", "接受客户");
	    	hs.put("BCE01", map.get("BCE01").toString());
	    	hs.put("BCE03", map.get("BCE03").toString());
	    	hs.put("AType", 2);
	    	hoschargeDao.HORate_ZAE1_Update(hs);
		  
		  return mo;
	  }
	  public void HOCRM_SCH1_Update(Map map)throws Exception{
		  if(map.get("SCH01")==null||Long.parseLong(map.get("SCH01").toString())<0){
			  map.put("SCH01", getNewId.nextId());
			  hosPatientDao.insertSCH1(map);
			  
			  
		  }
		  
		  
	  }
    public ModelVo inpatientList1(Map<String, Object> map)
    {
        ModelVo mo = new ModelVo();
        List list =null;
        try {
            list=hosPatientDao.queryForList("HosPatient.878_1", map);
//            mo.setList(list);
            mo.setRows(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        } catch (Exception e) {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
}
